package com.tmxk.springmvc.controller;

import com.tmxk.springmvc.constants.Result;
import com.tmxk.springmvc.pojo.TUserinfo;
import com.tmxk.springmvc.service.TUserinfoService;
import com.tmxk.springmvc.utils.ResultData;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

// @RestController 在类头上加注解,组合注解, @RestController = @Controller + @ResponseBody
@Controller
@RequestMapping("/user")
public class TUserinfoController {
  //  private Logger logger = Logger.getLogger(TUserinfoController.class);

    //创建Controller(控制层)和Service(业务层)关联关系
    @Resource
    private TUserinfoService tUserinfoService;

    // @ResponseBody : 响应请求体注解(返回[响应]的是JSON格式数据)
    @ResponseBody
    @RequestMapping(value = "/queryTUserinfoByInfo.do", method = RequestMethod.GET)
    /*public ResultData queryTUserinfoByInfo(Integer page , Integer limit,String userName,String city) {
        //创建TUserinfo实例对象,获取分页当前页数及每页显示的数量
        TUserinfo requestParam = new TUserinfo();
        requestParam.setStart(page,limit);
        requestParam.setEnd(page,limit);
        requestParam.setUserName(userName);
        requestParam.setCity(city);
        //查询
        List<TUserinfo> tUserinfos = tUserinfoService.queryTUserinfoByInfo(requestParam);
        //计算TUserinfo总数量(条数)
        int count = tUserinfoService.calcCount(requestParam);
        return new ResultData<>(Result.SUCCESS.getCode(), "", count, tUserinfos);
    }*/
    public ResultData queryTUserinfoByInfo(TUserinfo requestParam) {
        //创建TUserinfo实例对象,获取分页当前页数及每页显示的数量
        requestParam.setStart(requestParam.getPage(), requestParam.getLimit());
        requestParam.setEnd(requestParam.getPage(), requestParam.getLimit());
        //查询
        List<TUserinfo> tUserinfos = tUserinfoService.queryTUserinfoByInfo(requestParam);
        //计算TUserinfo总数量(条数)
        int count = tUserinfoService.calcCount(requestParam);
        return new ResultData<>(Result.SUCCESS.getCode(), "", count, tUserinfos);
    }

    @ResponseBody
    @RequestMapping("/queryUserInfoById.do")
    public ResultData queryUserInfoById(Integer id) {
        TUserinfo tUserinfo = new TUserinfo();
        tUserinfo.setId(id);
        List<TUserinfo> tUserinfos = tUserinfoService.queryTUserinfoByInfo(tUserinfo);
        return new ResultData<>(Result.SUCCESS.getCode(), "", tUserinfos);
    }

    @ResponseBody
    @RequestMapping(value = "/saveTUserinfo.do", method = RequestMethod.POST)
    public ResultData saveTUserinfo(TUserinfo tUserinfo) {
        boolean isSave = tUserinfoService.insertTUserinfo(tUserinfo);
        int code = isSave == true ? Result.SUCCESS.getCode() : Result.FAILED.getCode();
        String msg = isSave == true ? "添加成功!!!" : "添加失败!!!";
        return new ResultData<>(code, msg);
    }

    @ResponseBody
    @RequestMapping(value = "/updateTUserinfoById.do")
    public ResultData updateTUserinfoById(TUserinfo tUserinfo) {
        boolean isEdit = tUserinfoService.updateTUserinfoById(tUserinfo);
        int code = isEdit == true ? Result.SUCCESS.getCode() : Result.FAILED.getCode();
        String msg = isEdit == true ? "编辑成功!!!" : "编辑失败!!!";
        return new ResultData<>(code, msg);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteTUserinfoById.do", method = RequestMethod.DELETE)
    public ResultData deleteTUserinfoById(Integer id) {
        boolean isDel = tUserinfoService.deleteTUserinfoById(id);
        int code = isDel == true ? Result.SUCCESS.getCode() : Result.FAILED.getCode();
        String msg = isDel == true ? "删除成功!!!" : "删除失败!!!";
        return new ResultData<>(code, msg);
    }

    // MultipartFile : 文件类 , 根据该类获取上传文件信息
    //文件上传
    @ResponseBody
    @RequestMapping("/fileUploadPersonImg.do")
    public ResultData fileUploadPersonImg(MultipartFile file, HttpServletRequest request) {
        //获取request请求中文件信息
        // 1. 上传文件数据,数据库中一般存储文件路径,最终的文件通常放在文件服务中
        // file文件协议    http网络请求协议
        // 存储文件路径 相对路径   绝对路径
        //获取当前项目的绝对位置(路径)
        String path = request.getSession().getServletContext().getRealPath("upload");
        System.out.println("path:" + path);
 //       logger.info("path:" + path);
        if (file != null && !file.isEmpty()) {
            //获取multipartFile原文件名
            String fileName = file.getOriginalFilename();
            //根据原文件名更改新名称
            String newFileName = UUID.randomUUID().toString().substring(0, 6) + "." + FilenameUtils.getExtension(fileName);
            //创建文件
            File f = new File(path, newFileName);
            //判断当前文件路径是否存在
            if (!f.getParentFile().exists()) {
                //创建
                f.getParentFile().mkdirs();
            }
            try {
                //获取新文件名称即相对路径地址
                String imgUrl = "upload/" + newFileName;
                //保存文件
                file.transferTo(f);
                return new ResultData<>(Result.SUCCESS.getCode(), imgUrl);
            } catch (IOException e) {
               // throw new RuntimeException(e);
              //  logger.debug(e.getMessage());
                System.out.println(e.getMessage());
                return new ResultData<>(Result.FAILED.getCode(), "");
            }
        }
        return new ResultData<>();
    }
 }
